package com.xw.controller.manage;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import com.xw.common.Const;
import com.xw.model.*;
import com.xw.service.OrdersService;
import com.xw.service.TypeService;
import com.xw.service.UserInfoService;
import com.xw.service.VideosService;
import com.xw.service.sysconfig.DicGroupLimitService;
import com.xw.service.sysconfig.UserService;
import com.xw.util.JsonData;
import com.xw.util.PropertiesUtil;
import com.xw.vo.TypeVo;
import com.xw.vo.UserInfoVo;
import com.xw.vo.UserVo;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员 >> 注册用户表
 */
@Controller
@RequestMapping("/manage/webUser/")
@Scope("prototype")
public class UserWebController {
    private Map<String,Object> map = null;
    @Resource
    private UserService userService;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private DicGroupLimitService groupLimitService;
    @Resource
    private OrdersService ordersService;
    @Resource
    private TypeService typeService;
    @Resource
    private VideosService videosService;
    /**
     * 根据userId查询自己发布的已售出的订单
     * @param userId
     * @return
     */
    @RequestMapping(value = "findOrdersByPublishUserId",method = RequestMethod.GET)
    public String findOrdersByPublishUserId(HttpServletRequest request, String userId){
        request.setAttribute("userId",userId);
        return "orders/listPublishUserId";
    }

    @RequestMapping(value = "findOrdersByPublishUserIdJson",method = RequestMethod.GET)
    @ResponseBody
    public Object findOrdersByPublishUserIdJson(String userId,Integer page,Integer limit){
        Map map = ordersService.findOrdersByPublishUserId(userId,page,limit);
        return map;
    }

    /**
     * 跳转到讲师审核列表页面
     * @return
     */
    @RequestMapping(value = "findCheckTeacher",method = RequestMethod.GET)
    public String findCheckTeacher(HttpServletRequest request, String glId){
        DicGroupLimit model = groupLimitService.queryByUUID(glId);
        request.setAttribute("model",model);
        request.setAttribute(Const.URL, PropertiesUtil.getProperty("URL"));
        return "webUser/checkTeacherList";
    }


    /**
     * 获取列表数据
     * @return
     */
    @RequestMapping(value = "findCheckTeacherData",method = RequestMethod.GET)
    @ResponseBody
    public Object findCheckTeacherData(Integer page,Integer limit,String userNickName){
        map = Maps.newHashMap();
        map = userInfoService.findCheckTeacherData(page,limit,userNickName);
        return map;
    }

    @RequestMapping(value = "toNoPass",method = RequestMethod.GET)
    public String toNoPass(HttpServletRequest request, String glId,String uiId){
        DicGroupLimit model = groupLimitService.queryByUUID(glId);
        request.setAttribute("model",model);
        request.setAttribute("uiId",uiId);
        request.setAttribute(Const.URL, PropertiesUtil.getProperty("URL"));
        return "webUser/noPass";
    }

    @RequestMapping(value = "pass",method = RequestMethod.POST)
    @ResponseBody
    public Object pass(HttpServletRequest request, String uiId){
        map = Maps.newHashMap();
        map = userInfoService.pass(uiId);
        return map;
    }

    @RequestMapping(value = "noPass",method = RequestMethod.POST)
    @ResponseBody
    public Object noPass(HttpServletRequest request, String uiId,String noPassReason){
        map = Maps.newHashMap();
        map = userInfoService.noPass(uiId,noPassReason);
        return map;
    }

    /**
     * 拉黑
     * @param request
     * @param userId
     * @return
     */
    @RequestMapping(value = "joinBlack",method = RequestMethod.POST)
    @ResponseBody
    public Object joinBlack(HttpServletRequest request, String userId){
        map = Maps.newHashMap();
        map = userService.joinBlack(userId);
        return map;
    }

    /**
     * 解除拉黑
     * @param request
     * @param userId
     * @return
     */
    @RequestMapping(value = "removeJoinBlack",method = RequestMethod.POST)
    @ResponseBody
    public Object removeJoinBlack(HttpServletRequest request, String userId){
        Map map = Maps.newHashMap();
        map = userService.removeJoinBlack(userId);
        return map;
    }

    /**
     * @需求描述:辞退讲师
     * @创建人:whh
     * @创建时间: 2019/11/18
     * @修改需求:
     * @修改人:
     * @修改时间:
     * @需求思路:
     */
    @RequestMapping(value = "dismissTeacher",method = RequestMethod.POST)
    @ResponseBody
    public Object dismissTeacherAction(HttpServletRequest request, String userId){
        Map map = Maps.newHashMap();
        map = userService.dismissTeacher(userId);
        return map;
    }
    /**
     * @需求描述:指定讲师
     * @创建人:whh
     * @创建时间: 2019/11/18
     * @修改需求:
     * @修改人:
     * @修改时间:
     * @需求思路:
     */
    @RequestMapping(value = "makeTeacher",method = RequestMethod.POST)
    @ResponseBody
    public Object makeTeacherAction(HttpServletRequest request, String userId,String userDesc){
        Map map = Maps.newHashMap();
        map = userService.makeTeacher(userId,userDesc);
        return map;
    }

    /**
     * 跳转到后台管理员列表页面
     * @return
     */
    @RequestMapping(value = "findUserWeb",method = RequestMethod.GET)
    public String findAllAdmin(HttpServletRequest request, String glId){
        DicGroupLimit model = groupLimitService.queryByUUID(glId);
        request.setAttribute("model",model);
        request.setAttribute(Const.URL, PropertiesUtil.getProperty("URL"));
        return "webUser/list";
    }

    /**
     * @需求描述:跳转上传视频页面
     * @创建人:whh
     * @创建时间: 2019/11/19
     * @修改需求:
     * @修改人:
     * @修改时间:
     * @需求思路:
     */
    @RequestMapping(value = "uploadVideo",method = RequestMethod.GET)
    public String uploadVideo(HttpServletRequest request, String glId,String userId){
       // DicGroupLimit model = groupLimitService.queryByUUID(glId);
        //用户信息以及课程集
        Map<String,Object> map = new HashMap<>();
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setUserId(userId);
        map.put("userInfoVo",userInfoVo);

        map = userInfoService.findTeacherInfo(map);
        TypeVo typeVo = new TypeVo();
        typeVo.setStLevel(1);
        List<TypeVo> typeVos = typeService.findByWhere(typeVo);
        request.setAttribute("data",map.get("data"));
        request.setAttribute("type",typeVos);
        System.out.println("--------"+typeVos);
        System.out.println("----------"+map.get("data"));
        request.setAttribute(Const.URL, PropertiesUtil.getProperty("URL"));
        return "videos/uploadVideo";
    }



    @RequestMapping(value = "uploadVideoInfo",method = RequestMethod.GET)
    public String uploadVideoInfo(HttpServletRequest request, String videoId,String userId) {
        // DicGroupLimit model = groupLimitService.queryByUUID(glId);

        request.setAttribute("videoId", videoId);
        request.setAttribute("userId", userId);

        request.setAttribute(Const.URL, PropertiesUtil.getProperty("URL"));
        return "videos/uploadVideoInfo";
    }
    /**
     * @需求描述:跳转添加至课程页面
     * @创建人:whh
     * @创建时间: 2019/11/19
     * @修改需求:
     * @修改人:
     * @修改时间:
     * @需求思路:
     */
    @RequestMapping(value = "addToVideo",method = RequestMethod.GET)
    public String getTeacherListAction(HttpServletRequest request, String glId,String userId,String videoItemId){

        //讲师集合
        Map<String,Object> map = new HashMap<>();
        map = userInfoService.showTeacherList();
        request.setAttribute("data",map.get("data"));
        request.setAttribute("videoItemId",videoItemId);
        request.setAttribute("userId",userId);
        System.out.println("------------讲师列表----------"+ map.get("data"));
        request.setAttribute(Const.URL, PropertiesUtil.getProperty("URL"));
        return "videosItem/addToVideo";
    }


    /**
     * @需求描述:查找讲师课程集合
     * @创建人:whh
     * @创建时间: 2019/11/23
     * @修改需求:
     * @修改人:
     * @修改时间:
     * @需求思路:
     */
    @RequestMapping(value = "getTeacherVideo",method = RequestMethod.POST)
    @ResponseBody
    public Object getTeacherVideoListAction(HttpServletRequest request, String userId,String videoItemId,Integer page,Integer limit){
        //讲师课程集合
        Map<String,Object> map =  videosService.getAllVideos(userId, page, limit);
        map.put("videoItemId",videoItemId);
       // request.setAttribute("data",map.get("videosList"));
        request.setAttribute("videoItemId",videoItemId);

        return map;
    }



    /**
     * 获取列表数据
     * @return
     */
    @RequestMapping(value = "findUserWebData",method = RequestMethod.GET)
    @ResponseBody
    public Object findAllAdminData(HttpSession session, UserVo user,Integer page,Integer limit){
        //1.用户 2.系统用户
        user.setUserType(1);
        Integer count=userService.findByWhere(user).size();

        if (page != null && limit != null){
            PageHelper.startPage(page,limit,false);
        }

        List<UserVo> list=userService.findByWhere(user);
        return JsonData.returnLayuiMap(list, count);
    }


    /**
     * 删除
     * @return
     */
    @RequestMapping(value = "del",method = RequestMethod.POST)
    @ResponseBody
    public Object del(String id){
        Integer integer = userService.deleteByUUId(id);
        return JsonData.returnMap(integer);
    }


    /**
     * @需求描述:讲师提现
     * @创建人:whh
     * @创建时间: 2019/11/23
     * @修改需求:
     * @修改人:
     * @修改时间:
     * @需求思路:
     */
    @RequestMapping(value = "cashOut",method = RequestMethod.POST)
    @ResponseBody
    public Object cashOutAction(String userId, String balance ,String cashOut){
         Double oldBalance = Double.parseDouble(balance);
        Double oldCashOut = Double.parseDouble(cashOut);
        if (oldCashOut<=0){
            return JsonData.fail("请输入大于0的正数(保留两位小数)");
        }
         if (oldCashOut > oldBalance){
             return JsonData.fail("提现金额不能大于讲师余额");
         }
        Double newBalance = oldBalance-oldCashOut;

        return userService.cashOutBalance(userId,newBalance,oldCashOut);
    }



}
